package com.semlab.client.widgets;

import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.semlab.client.bundle.Resources;

/**
 * GetIDNg hyperlink
 */
public class Hyperlink extends HTML {

	private static String color = "#0090FF";
	private static String mouseOverColor = "#67A5FF";

	private final static MouseOverHandler mouseOverHandler = new MouseOverHandler() {
		@Override
		public void onMouseOver(MouseOverEvent event) {
			((Widget) event.getSource()).getElement().getStyle()
					.setColor(mouseOverColor);
		}
	};
	private final static MouseOutHandler mouseOutHandler = new MouseOutHandler() {
		@Override
		public void onMouseOut(MouseOutEvent event) {
			((Widget) event.getSource()).getElement().getStyle()
					.setColor(color);
		}
	};

	public Hyperlink() {
		this("", null);
	}

	public Hyperlink(String html) {
		this(html, null);
	}
	
	public Hyperlink(final String uri, boolean reference) {
		this(uri, null);
		if(reference){
			addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent event) {
					Window.open(uri, "_blank", "");
				}
			});
		}
	}

    public Hyperlink(String name, final String uri, boolean reference) {
        this(name, null);
        if(reference){
            addClickHandler(new ClickHandler() {

                @Override
                public void onClick(ClickEvent event) {
                    Window.open(uri, "_blank", "");
                }
            });
        }
    }

	public Hyperlink(String title, ClickHandler clickHandler) {
		this(title, color, mouseOverHandler, mouseOutHandler);
		if (clickHandler != null) {
			addClickHandler(clickHandler);
		}
	}

	public Hyperlink(String title, String color, MouseOverHandler over,
			MouseOutHandler out) {
		super(title);
		addMouseOverHandler(over);
		addMouseOutHandler(out);
		getElement().getStyle().setColor(color);
		addStyleName(Resources.INSTANCE.css().hand());
		getElement().getStyle().setDisplay(Display.INLINE);
	}


    public void setText(String text, int num) {
		setText(text+" ("+num+")");
	}

}
